//聊天
var basePage = require('../../../common/BasePage');
// var baseList = require('../../../common/BaseList');
var app = getApp()
var SocketTask = app.globalData.SocketTask;
var socketOpen = false;
Page({
	behaviors: [basePage],
	data: {
		activeNames: '1',
		friendid: 1,
		friendList: [],
		friendContentList: [],
        myInfo: {},
        wh_id:0,
		friendindex:0
	},
	onLoad: function(options) {
        this.setData({
            wh_id:options.wh_id||1
        })
	},
	onShow: function() {
        // this.getMyfriend()
        this.getMyfriend2()
	},
	//退出页面，关闭socket
	onUnload: function() {
		// app.globalData.SocketTask.close(function(close) {
		// 	console.log('关闭 WebSocket 连接。', close)
		// })
	},
	webSocket: function() {
		getApp().globalData.onMessage = this.onMessage;
	},
	onMessage(msg) {
		console.log("未读聊天数量", msg)
		var that = this
		// if(msg.type=='msg_count'){
		// 	var friendList=this.data.friendList
		// 	for(var i=0;i<friendList.length;i++){
		// 		for(var x=0;x<friendList[i].list.length;x++){
		// 			if(friendList[i].list[x].id==msg.id){
		// 				friendList[i].list[x].msg_count=msg.msg_count
		// 			}
		// 		}
		// 	}
		// 	console.log("未读数量",friendList)
		// 	this.setData({
		// 		friendList:friendList
		// 	})
		// }else
		if (msg.type == 'friend') {

			var friendList = this.data.friendList
			var friendContentList = this.data.friendContentList
			console.log("****", friendContentList)
			for (var i = 0; i < friendList.length; i++) {
				for (var x = 0; x < friendList[i].list.length; x++) {
					if (friendList[i].list[x].id == msg.id) {

						friendList[i].list[x].msg_count = friendList[i].list[x].msg_count + 1
						friendList[i].msgCount = friendList[i].msgCount + 1

					}
				}
			}
			// for(var j=0;j<friendContentList.length;j++){
			//  if(friendContentList[j].id==msg.id){
			// 	 friendContentList[j].msg_count=friendContentList[j].msg_count+1
			//  }
			// }
			console.log("未读数量", friendList)
			this.setData({
				friendList: friendList,
				friendContentList: friendContentList
			})
		}

    },
    getMyfriend2() {
		console.log("仓库角标",this.data.friendindex)
		this.request({
			url: this.data.host + "/home/chat/friend2?wh_id="+this.data.wh_id
		}, res => {
            var result=res.data
            var friendList =[]
            if(result.now_city){//本地仓
                friendList.push(result.now_city)
            }
            if(result.now_district){//关联仓
                friendList.push(result.now_district)
            }
            if(result.platform){//总仓
                friendList.push(result.platform)
            }
            
            
            
            for (var i = 0; i < friendList.length; i++) {
				friendList[i].msgCount = 0
				for (var j = 0; j < friendList[i].list.length; j++) {
					if (friendList[i].list[j].msg_count > 0) {
						friendList[i].msgCount = friendList[i].msgCount + friendList[i].list[j]
							.msg_count
					}
				}
            }
            console.log('仓库列表friendList2', friendList)
            var friendid = 1
            if(friendList.length>0){
                friendid=friendList[0].id
            }
            var friendContentList=[]
			if(this.data.friendindex==-1){
				friendContentList=friendList[friendList.length - 1].list
			}else{
				friendContentList=friendList[this.data.friendindex].list
			}
			console.log('仓库列表2', friendList)
			console.log("朋友列表2", friendContentList)
			this.setData({
                friendid: friendid,
				friendList: friendList,
				myInfo: res.data.mine_data,
				friendContentList: friendContentList
            })
            console.log('仓库列表21', this.data.friendList)
			console.log("朋友列表21", this.data.friendContentList)
			this.webSocket()
		})
	},
	getMyfriend() {
		console.log("仓库角标",this.data.friendindex)
		this.request({
			url: this.data.host + "/home/chat/friend"
		}, res => {
			console.log("我的朋友", res)
			var friendList = res.data.friend
			for (var i = 0; i < friendList.length; i++) {
				friendList[i].msgCount = 0
				for (var j = 0; j < friendList[i].list.length; j++) {
					if (friendList[i].list[j].msg_count > 0) {
						friendList[i].msgCount = friendList[i].msgCount + friendList[i].list[j]
							.msg_count
					}
				}
            }
            
            
			var friendContentList=[]
			if(this.data.friendindex==-1){
				friendContentList=friendList[friendList.length - 1].list
			}else{
				friendContentList=friendList[this.data.friendindex].list
            }
            
			console.log('仓库列表', friendList)
			console.log("朋友列表", friendList[friendList.length - 1])
			this.setData({
                
				friendList: friendList,
				myInfo: res.data.mine,
				friendContentList: friendContentList
			})
			this.webSocket()
		})
	},
	//切换仓库
	changeFriend(e) {
		console.log("切换仓库", e)
		var friendContentList = []
		var friendid = 1
		var index = e.currentTarget.dataset.index
		if (index == -1) {
			friendContentList = this.data.friendList[this.data.friendList.length - 1].list
			friendid = this.data.friendList[this.data.friendList.length - 1].id
		} else {
			friendContentList = this.data.friendList[index].list
			friendid = this.data.friendList[index].id
		}
		this.setData({
			friendContentList: friendContentList,
			friendid: friendid,
			friendindex:index
		})
	},
	changeCollapse(e) {
		console.log("changeCollapse", e)
		this.setData({
			activeNames: e.detail
		})
	},
	//跳转聊天
	clickChat(e) {
		console.log("socket连接状态",getApp().globalData.showSocket)
		if(!getApp().globalData.showSocket){
			wx.showModal({
			  title: '客服系统已断开',
			  content: '请确定是否重连',
			  success (res) {
			    if (res.confirm) {
			      console.log('用户点击确定')
				  getApp().globalData.number=0
				  getApp().globalData.againSocket=1
				  getApp().connectSocket();
				  
			    } else if (res.cancel) {
			      console.log('用户点击取消')
			    }
			  }
			})
			return;
		}
		if (getApp().globalData.number >= 3) {
			wx.showToast({
				title: '聊天服务异常，请重启小程序或者联系客服',
				icon: 'none',
				duration: 2000
			})
			return;
		}
		var readmsg = {
			type: 'read_msg',
			id: this.data.myInfo.id,
			toid: e.currentTarget.dataset.userinfo.id
		}
		app.globalData.SocketTask.send({
			data: JSON.stringify(readmsg),
			success: function(res) {
				console.log('发送成功', res)
			},
			fail: function(err) {
				console.log("发送失败", err)
				// that.webSocket()

			},
		})
		console.log("聊天", e)
		wx.navigateTo({
			url: '/pages/chat/chat?serviceInfo=' + JSON.stringify(e.currentTarget.dataset.userinfo) +
				"&myInfo=" + JSON.stringify(this.data.myInfo),
		})
	},
	callPhone(e) {
		console.log("拨打电话", e)
		var phone=e.currentTarget.dataset.phone
		if(!phone){
			wx.showToast({
				title: '此客服暂无联系电话',
				icon: 'none',
				duration: 2000
			})
			return;
		}
		wx.makePhoneCall({

			phoneNumber: phone,

		})
	}
})
